package com.txf.simple.tools;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.util.Log;

import java.io.File;

public class StoreToadTools {
    private static final String TAG = "StoreToadTools";
    private static StoreToadTools mStoreToadtools;
    private int VERSION_CODE;//App版本号
    private String VERSION_NAME;//App版本
    private String imgePath;//图片存放路劲
    private String interimPath;//临时存放路劲
    private String pagePath;//页面缓存路劲
    private String userInfoPath;//用户信息存放路劲
    private String userNewsPath;//用户聊天记录存放路劲
    public static StoreToadTools getInstance() {
        if (mStoreToadtools == null)
            mStoreToadtools = new StoreToadTools();
        return mStoreToadtools;
    }
    /**
     * 初始化
     * */
    public void init(Context paramContext) {
        this.interimPath = autoBuildCostomPath(paramContext, "interim");
        this.imgePath = autoBuildCostomPath(paramContext, "imge");
        this.pagePath = autoBuildCostomPath(paramContext, "page");
        this.userInfoPath = autoBuildCostomPath(paramContext, "userInfo");
        this.userNewsPath = autoBuildCostomPath(paramContext, "userNews");
        this.VERSION_CODE = getVersionCode(paramContext);
        this.VERSION_NAME = getVersionName(paramContext);

        Logger.i("APP信息:" +
                "\n............................................" +
                "\nAPP版本号 : " + this.VERSION_CODE +
                "\nAPP版本名称 : " + this.VERSION_NAME +
                "\nAPP临时存放 : " + this.interimPath +
                "\nAPP图片存放 : " + this.imgePath +
                "\nAPP页面存放 : " + this.pagePath +
                "\nAPP用户详情存放 : " + this.userInfoPath +
                "\nAPP用户聊天记录存放 : " + this.userNewsPath +
                "\n............................................");
    }
    public String getImgePath() {
        return this.imgePath;
    }

    public String getInterimPath() {
        return this.interimPath;
    }

    public String getPagePath() {
        return this.pagePath;
    }

    public String getUserInfoPath() {
        return this.userInfoPath;
    }

    public String getUserNewsPath() {
        return this.userNewsPath;
    }

    public int getVERSION_CODE() {
        return this.VERSION_CODE;
    }

    public String getVERSION_NAME() {
        return this.VERSION_NAME;
    }

    public int getVersionCode(Context paramContext) {
        try {
            int i = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0).versionCode;
            return i;
        } catch (NameNotFoundException localNameNotFoundException) {
            Log.i(TAG,"版本号获取失败");
//            localNameNotFoundException.printStackTrace();
        }
        return 0;
    }
    public String getVersionName(Context paramContext) {
        try {
            String str = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0).versionName;
            return str;
        } catch (NameNotFoundException localNameNotFoundException) {
            Log.i(TAG,"版本名字获取失败");
//            localNameNotFoundException.printStackTrace();
        }
        return "exception";
    }
    private String autoBuildCostomPath(Context paramContext, String paramString) {
        String str = buildSdcardPath(paramString);
        if ((str == null) || (str.length() == 0))
            str = buildPrivateDataDir(paramContext, paramString);
        return str;
    }
    private String buildPrivateDataDir(Context paramContext, String paramString) {
        return paramContext.getDir(paramString, 0).toString() + File.separator;
    }
    private String buildSdcardPath(String paramString) {
        if (Environment.getExternalStorageState().equals("mounted")) {
            String str = Environment.getExternalStorageDirectory().toString() + File.separator + "TANG" + File.separator + paramString + File.separator;
            File localFile = new File(str);
            if (!localFile.exists())
                localFile.mkdirs();
            return str;
        }
        return "";
    }
}